'use strict';

const crypto = require('crypto')
if(!global.SESSION) global.SESSION = {};
/** 权限控制 */
module.exports = (request, response, rfun)=>{
    request.COOKIE = request.PUBFUN.getCookie(request);
    if(!("SID" in request.COOKIE)){
        let md5 = crypto.createHash("md5");
        md5.update(Date.now().toString());
        request.COOKIE["SID"] = md5.digest('hex');
        response.setHeader("Set-Cookie", "SID="+request.COOKIE["SID"]+";path=/");
        SESSION[request.COOKIE["SID"]] = {};
    }
    request.SESSION = SESSION[request.COOKIE["SID"]];
    if(!request.SESSION){
        request.SESSION = SESSION[request.COOKIE["SID"]] = {};
    }
    //不是登录界面的时都需要权限判断
    if(request.GET.pathname != "/s/login" && request.GET.pathname != "/s/loginout"){
        if(!(request.SESSION.path && request.GET.pathname in request.SESSION.path)){
            response.setHeader("Content-Type", "application/json");
            response.end('{"location":"/login.html"}');
            rfun();
            return;
        }
    }
    switch(request.GET.pathname){
        case "/s/login":{ //登录
            let user = require("../config/" + request.SITE.key + "user.json");
            let ret = {code:1};
            if(request.GET.query.U in user.user){
                let md5 = crypto.createHash("md5");
                md5.update(request.GET.query.P);
                let pw = md5.digest('hex');
                console.log("pw", pw, user.user[request.GET.query.U].P);
                if(pw == user.user[request.GET.query.U].P){
                    ret.code = 0;
                    request.SESSION.role = user.user[request.GET.query.U].R;
                    request.SESSION.N = user.user[request.GET.query.U].N;
                    request.SESSION.path = {};
                    request.SESSION.path["/s/menu"] = 1;
                }
            }
            response.DATA = ret
            break;
        }case "/s/loginout":{ //退出
            let ret = {code:1};
            delete request.SESSION.role;
            delete request.SESSION.path;
            response.DATA = ret
            break;
        }case "/s/menu":{ //获取权限菜单
            const menu = require("../config/" + request.SITE.key + "menu.json"), user = require("../config/" + request.SITE.key + "user.json");
            let smenu = [], ro = user.role[request.SESSION.role];
            for(let i=0;i<menu.length;i++){
                if(!(request.SESSION.role == 0 || i in ro.M)) continue;
                let mitem = {"icon":menu[i].icon,"index":menu[i].index,"title":menu[i].title,"list":[]};
                for(let j=0;j<menu[i].list.length;j++){
                    if(!(request.SESSION.role == 0 || j in ro.M[i])) continue;
                    request.SESSION.path["/ajax/" + menu[i].list[j].path] = 1;
                    mitem.list.push(menu[i].list[j]);
                }
                smenu.push(mitem);
            }
            response.DATA = smenu;
            break;
        }
    }
    //console.log(SESSION, request.COOKIE["tjsid"], request.SESSION);
    rfun();
}